package com.cxs.tetrisdemo.slice;

import com.cxs.tetrisdemo.ResourceTable;
import com.cxs.tetrisdemo.tetris.Tetris;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.multimodalinput.event.KeyEvent;

import static com.cxs.tetrisdemo.common.Constant.*;

public class MainAbilitySlice extends AbilitySlice {
    DirectionalLayout mainLayout;   // 根布局
    DirectionalLayout hLayout;      // 水平布局，存放Tetris和控制组件1
    Tetris tetris;
    DirectionalLayout controlLayout1; // 控制组件1，开始/暂停/再来一局
    DirectionalLayout controlLayout2; // 控制组件2，左/下/右/变形
    Button btnStart, btnPause, btnLeft, btnDown, btnRight, btnRorate;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        // 根布局
        mainLayout = new DirectionalLayout(this);
        mainLayout.setOrientation(Component.VERTICAL);   // 垂直布局

        // 水平布局用来存储俄罗斯方块组件和开始/暂停等按钮
        hLayout = new DirectionalLayout(this);
        hLayout.setOrientation(Component.HORIZONTAL);    // 水平布局

        // 俄罗斯方块组件
        tetris = new Tetris(this, 80);
        DirectionalLayout.LayoutConfig layoutConfig =
                new DirectionalLayout.LayoutConfig(800, 1600);
        layoutConfig.setMarginLeft(10);
        layoutConfig.setMarginTop(10);
        tetris.setLayoutConfig(layoutConfig);

        // 控制组件1，开始/暂停/再来一局
        controlLayout1 = (DirectionalLayout)LayoutScatter
                .getInstance(this).parse(ResourceTable.Layout_control_layout_1, null, false);
        // 控制组件2，左/下/右/变形
        controlLayout2 = (DirectionalLayout)LayoutScatter
                .getInstance(this).parse(ResourceTable.Layout_control_layout_2, null, false);

        hLayout.addComponent(tetris);
        hLayout.addComponent(controlLayout1);

        mainLayout.addComponent(hLayout);
        mainLayout.addComponent(controlLayout2);
        super.setUIContent(mainLayout);

        initListener();
    }

    void initListener() {
        btnStart = (Button)controlLayout1.findComponentById(ResourceTable.Id_btn_start);
        btnStart.setClickedListener(c -> {
            tetris.startGame();
            c.setEnabled(false);
            btnPause.setEnabled(true);
        });

        btnPause = (Button)controlLayout1.findComponentById(ResourceTable.Id_btn_pause);
        btnPause.setClickedListener(c -> {
            tetris.pauseGame();
            c.setEnabled(false);
            btnStart.setEnabled(true);
        });

        controlLayout1.findComponentById(ResourceTable.Id_btn_retry).setClickedListener(c -> {

        });

        btnLeft = (Button)controlLayout2.findComponentById(ResourceTable.Id_btn_left);
        btnLeft.setClickedListener(c -> {
            tetris.moveTetrisBlock(MOVE_LEFT);
        });

        btnDown = (Button)controlLayout2.findComponentById(ResourceTable.Id_btn_down);
        btnDown.setLongClickedListener(c -> {
            tetris.moveTetrisBlock(MOVE_DOWN);
        });


        btnRight = (Button)controlLayout2.findComponentById(ResourceTable.Id_btn_right);
        btnRight.setClickedListener(c -> {
            tetris.moveTetrisBlock(MOVE_RIGHT);
        });

        btnRorate = (Button)controlLayout2.findComponentById(ResourceTable.Id_btn_rorate);
        btnRorate.setClickedListener(c -> {
            tetris.rotateTetrisBlock();
        });
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
